#include "mainwindow.h"

#include "kdcconfigdialog.h"
#include <kdclib.h>
#include "kdcstatusbar.h"

#include <QFileInfoList>
#include <QDir>
#include <QList>
#include <QDebug>

#include <KStandardAction>
#include <KXMLGUIFactory>
#include <KActionMenu>
#include <KSelectAction>
#include <KActionCollection>
#include <KStandardShortcut>
#include <KLocale>
#include <KPluginFactory>
#include <KPluginLoader>
#include <KPluginInfo>
#include <ktoolbar.h>
#include <interfaces/kdcframegeneratorinterface.h>

KDCApplicationInterface *gKDCApplicationInterface;

MainWindow::MainWindow( QWidget *parent ) : KXmlGuiWindow( parent )
{
	gKDCApplicationInterface = new KDCApplicationInterfaceImpl;
	p_interfaces.append( gKDCApplicationInterface );
	setWindowIcon( gKDCApplicationInterface->appIcon() );

	p_mainWidget = new KDCMainWindowFrame( this );
	setCentralWidget( p_mainWidget );
	layout()->setContentsMargins( 0, 0, 0, 0 );
	layout()->setSpacing( 0 );
	
	KDCStatusBar *statusBar = new KDCStatusBar( this );
	setStatusBar( statusBar );
	p_interfaces.append( statusBar );
	p_interfaces.append( this );
	
	toolBar()->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
	createActions();
	setupGUI();
	loadPlugins();
}

MainWindow::~MainWindow()
{/*
	foreach ( KDCPluginInterface *plugin, p_plugins )
	{
		p_mainWidget->removePlugin( plugin );
		plugin->unload();
		deletePtr( plugin );
	}
	*/
}

void MainWindow::createActions()
{
	KStandardAction::preferences( this, SLOT( slotPreferences() ), actionCollection() );
	
	KAction *action;
	action = actionCollection()->addAction( "quit", this, SLOT( close() ) );
	action->setShortcut( KStandardShortcut::shortcut( KStandardShortcut::Quit ) );
	action->setIcon( KIcon("application-exit") );
	action->setText( i18n("Quit") );
}

bool MainWindow::loadPlugins()
{
	QDir path( gKDCApplicationInterface->applicationDirPath() + "/plugins/" );
	QFileInfoList dirList = path.entryInfoList( QDir::Files );
	QList<QFileInfo>::const_iterator it;
	KPluginFactory *factory;
	KDCPluginInterface *plugin;
	QList<KDCPluginInterface*> plugins;
	for ( it = dirList.constBegin(); it != dirList.constEnd(); ++it )
	{
		factory = KPluginLoader( (*it).absoluteFilePath() ).factory();
		if ( factory )
		{
			plugin = factory->create<KDCPluginInterface>();
			if ( plugin )
			{
				emit loadPlugin( plugin->info().name() );
				if ( !plugin->load() )
					return false;
				if ( !plugin->setInterfaces( p_interfaces ) )
					return false;
				plugins.append( plugin );
				p_interfaces.append( plugin );
			}
		}
	}
	foreach ( plugin, plugins )
	{
		if ( plugin->init() )
			p_mainWidget->addPlugin( plugin );
	}
	return true;
}

void MainWindow::slotPreferences()
{
	QList<KDCPluginInterface*> plugins;
	KDCPluginInterface *plugin;
	QList<KPluginInfo> externalPlugins;
	foreach ( KDCBaseInterface *interface, p_interfaces )
	{
		plugin = dynamic_cast<KDCPluginInterface*>( interface );
		if ( plugin )
			plugins.append( plugin );
		if ( interface->interfaceName() == "KDCFrameGeneratorInterface" )
			externalPlugins.append( static_cast<KDCFrameGeneratorInterface*>( interface )->pluginList() );
	}
	KDCConfigDialog dlg( this );
	dlg.pluginPage()->setKDCPlugins( plugins );
	dlg.pluginPage()->addPlugins( externalPlugins );
	dlg.loadSettings();
	if ( dlg.exec() )
		dlg.saveSettings();
}

void MainWindow::addGuiPlugin( KXMLGUIClient *aPlugin )
{
	guiFactory()->addClient( aPlugin );
}

void MainWindow::removeGuiPlugin( KXMLGUIClient *aPlugin )
{
	guiFactory()->removeClient( aPlugin );
}
